package com.jiangnanxiaoguan.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 不愿透露姓名的狂热者
 * @version 1.0
 */
@Slf4j
@Component
public class PublicMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long currentUserId = BaseContext.getCurrentId();
        metaObject.setValue("createUser", currentUserId);
        metaObject.setValue("updateUser", currentUserId);
        log.info("管理端当前用户id：{}", currentUserId);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]");
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long currentUserId = BaseContext.getCurrentId();
        log.info("管理端当前用户id：{}", currentUserId);
        metaObject.setValue("updateUser", currentUserId);
    }
}
